﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.Text;
using System.Data;

namespace FastDeal.Control
{
    public partial class KhuyenMaiTheoLoai1 : System.Web.UI.UserControl
    {

        KhuyenMai _makhuyenmai = new KhuyenMai();

        public KhuyenMai Makhuyenmai
        {
            get { return _makhuyenmai; }
            set { _makhuyenmai = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "ohMua! Khuyến Mãi Theo Loại ";
            try
            {
                int loai = int.Parse(Request.QueryString["MaLoaiKhuyenMai"].ToString());
                LayDanhSachTheoTheLoai(loai);
            }
            catch
            {
                LayDanhSachTheoTheLoai(0);
            }
        }

        private void LayDanhSachTheoTheLoai(int loai)
        {
           IQueryable loai1;
           KhuyenMai_BUS db = new KhuyenMai_BUS();
           loai1 = db.LayDanhSachTheoTheLoai(loai);
           DTlist.DataSource = loai1; // lấy datasource lấy hết luôn chủ yếu trong giao diên mình lấy sao nó sẽ lấy cho mình
           DTlist.DataBind();
           if (DTlist.Items.Count == 0)
           {
               Response.Redirect("~/HienThiTheoLoai.aspx");
           }

           
            int i=0;
           foreach (DataListItem dr in DTlist.Items)
           {
               DateTime dtbatdau = DateTime.Parse((((Label)dr.FindControl("lbThoiGianBatDau")).Text));
               DateTime dtketthuc = DateTime.Parse((((Label)dr.FindControl("lbThoiGianKetThuc")).Text));
               int tongsonguoimua = int.Parse((((Label)dr.FindControl("lbSoLuongNguoiMua")).Text));
               int soluongtoida = int.Parse((((Label)dr.FindControl("lbSoLuongToiDa")).Text));

               if (dtketthuc > DateTime.Now && soluongtoida >= tongsonguoimua)
               {
                   ((Image)DTlist.Items[i].FindControl("image_ChayHang")).Visible = true;
               }
               else
               {
                   ((Image)DTlist.Items[i].FindControl("image_ChayHang")).Visible = false;
               }
               i=i+1;
           }

           if (DTlist.Items.Count < 5)
           {
               Panel1.Height = 800;
           }

    

        }

        protected void DTlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        protected void DTlist_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Xem")
            {
                StringBuilder str = new StringBuilder();
    
                Int16 MaKhuyenMai = Convert.ToInt16(e.CommandArgument);
                string url = "~/ChiTietKhuyenMai.aspx?ID=" + _makhuyenmai.ToString();
                Response.Redirect(url);
            }
        }

        protected void btnXem_Click(object sender, EventArgs e)
        {

        }
    }
}